package cn.itsource.hrm.controller;

import cn.hutool.core.util.ZipUtil;
import cn.itsource.hrm.client.FileClient;
import cn.itsource.hrm.util.VelocityUtils;
import cn.itsource.util.AjaxResult;
import cn.itsource.util.VerifyCodeUtils;
import feign.Response;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import sun.nio.ch.IOUtil;
import org.apache.commons.fileupload.disk.DiskFileItem;

import java.io.*;
import java.nio.file.Files;
import java.util.Map;

/**
 * @author chenyuqi
 * @version v1.0.0
 * @date 2020/5/30
 */
@RestController
public class StaticPageController {

    //获取指定文件的配置
    @Value("${template.path}")
    private String path;
    @Autowired
    private FileClient fileClient;

    /**
     *
     * @param fileID 模板文件id
     * @param data 模板需要的数据
     * @return
     */
    @PostMapping("/staticPage")
    public AjaxResult staticPage(@RequestParam("fileID") String fileID, @RequestBody Map<String,Object> data){
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            //根据fileid从fastdfs中下载模板压缩包
            Response response = fileClient.download(fileID);
            inputStream = response.body().asInputStream();
            outputStream = new FileOutputStream(path+"static.zip");
            IOUtils.copy(inputStream,outputStream);
            //解压压缩包
            File unzip = ZipUtil.unzip(path+"static.zip",path);


            //页面静态化操作
            String templatePath= path+"home.vm";
            String targetPath = path+"home.html";
            //data中存入staticRoot,为模板所在根目录
            data.put("staticRoot",path);
            VelocityUtils.staticByTemplate(data,templatePath,targetPath);

            //将静态化页面上传到fastdfs中
            File f = new File(path+"home.html");
            FileItem fileItem = new DiskFileItem("file", Files.probeContentType(f.toPath()), false, f.getName(), (int) f.length(), f.getParentFile());
            InputStream in = new FileInputStream(f);
            OutputStream os = fileItem.getOutputStream();
            IOUtils.copy(in, os);
            MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
            AjaxResult result = fileClient.upload(multipartFile);

            return AjaxResult.me().setSuccess(true).setMessage("成功").setResultObj(result.getResultObj());
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("失败"+e.getMessage());
        } finally {
            try {
                if (outputStream!=null){
                    outputStream.close();
                }
                if (inputStream!=null){
                    inputStream.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
